package cate.game.play.buff.h;

import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 生命值低于一定程度添加 buff
 */
public class 残血添加buffBH extends BuffHandler {

	@NoteField("添加配置列表")
	public List<Config> buffAddList = new ArrayList<>();

	//buff=123&数量=1&血量=5000&触发次数=1,buff=123&数量=1&血量=5000&触发次数=1
	public void parse(String code) {
		if (StringUtils.isNotBlank(code)) {
			for (String s : code.split(",")) {
				buffAddList.add(new Config(s));
			}
		}
	}

	@Override
	public void onAdd(ActionCtx action) {
		settle(action);
	}

	@Override
	public void onMoveDone(MoveCtx move, TimeActionCtx action) {
		settle(action);
	}

	private void settle(ActionCtx action) {
		if (buff.owner.isDead()) {
			return;
		}
		int hpCurRate = buff.owner.attr.total().hpCurRate();
		for (Config cfg : buffAddList) {
			if (cfg.needAdd(hpCurRate)) {
				buff.owner.buff.tryAddSome(action, buff.caster, cfg.buffTid, cfg.num);
				cfg.curAdd++;
			}
		}
	}


	public static class Config {

		@NoteField("buff")
		public int buffTid;

		@NoteField("数量")
		public int num;

		@NoteField("血量")
		public double hpRate;

		@NoteField("触发次数")
		public int addMax;


		//buff=123&数量=1&血量=5000&触发次数=1
		public void parse(String code) {
			PlayCfgArgs args = new PlayCfgArgs(code);
			buffTid = args.getInt("buff", 0);
			num = args.getInt("数量", 1);
			hpRate = args.getInt("血量", 0);
			addMax = args.getInt("触发次数", 0);
		}

		private int curAdd;

		public Config(String code) {
			parse(code);
		}

		public boolean needAdd(double curHpRate) {
			if (addMax > 0 && curAdd >= addMax) {
				return false;
			}
			return curHpRate <= hpRate;
		}
	}
}
